/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * KegSplitFrame.java
 *
 * Created on Jun 7, 2011, 1:20:11 PM
 */

package gbbmgmt;

import org.jdesktop.application.Action;
import java.util.*;
import javax.swing.JOptionPane;

/**
 *
 * @author dchapman
 */
public class KegSplitFrame extends javax.swing.JFrame {

    public ArrayList<String[]> Stock;

    /** Creates new form KegSplitFrame */
    public KegSplitFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBoxBeer = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldNumHalf = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldNumQtr = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldNumSix = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabelStock = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jButtonSubmitMovement = new javax.swing.JButton();
        jButtonExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getResourceMap(KegSplitFrame.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jComboBoxBeer.setName("jComboBoxBeer"); // NOI18N
        jComboBoxBeer.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBoxBeerPopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextFieldNumHalf.setText(resourceMap.getString("jTextFieldNumHalf.text")); // NOI18N
        jTextFieldNumHalf.setName("jTextFieldNumHalf"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jTextFieldNumQtr.setName("jTextFieldNumQtr"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jTextFieldNumSix.setName("jTextFieldNumSix"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabelStock.setText(resourceMap.getString("jLabelStock.text")); // NOI18N
        jLabelStock.setName("jLabelStock"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getActionMap(KegSplitFrame.class, this);
        jButtonSubmitMovement.setAction(actionMap.get("Submit")); // NOI18N
        jButtonSubmitMovement.setText(resourceMap.getString("jButtonSubmitMovement.text")); // NOI18N
        jButtonSubmitMovement.setName("jButtonSubmitMovement"); // NOI18N

        jButtonExit.setAction(actionMap.get("ExitKegSplit")); // NOI18N
        jButtonExit.setText(resourceMap.getString("jButtonExit.text")); // NOI18N
        jButtonExit.setName("jButtonExit"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxBeer, 0, 287, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldNumHalf, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelStock))
                    .addComponent(jLabel4))
                .addContainerGap(89, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldNumQtr, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldNumSix, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonSubmitMovement)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 181, Short.MAX_VALUE)
                .addComponent(jButtonExit)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBoxBeer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldNumHalf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(jLabelStock))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldNumQtr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jTextFieldNumSix, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSubmitMovement)
                    .addComponent(jButtonExit))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxBeerPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBoxBeerPopupMenuWillBecomeInvisible
        // TODO add your handling code here:
        String Selection = jComboBoxBeer.getSelectedItem().toString();
        DBA.openDBCon();

        for(String s[] : Stock){
            DBA.GetBeerData(s[1]);
            String desc = DBA.BName + " - " + s[2];
            if(Selection.equals(desc)){
                DBA.GetBeerInventoryData(s[0]);
                jLabelStock.setText(Integer.toString(DBA.BIHalf));
            }
        }

        jTextFieldNumHalf.setText("0");
        jTextFieldNumQtr.setText("0");
        jTextFieldNumSix.setText("0");

        DBA.closeDBCon();
    }//GEN-LAST:event_jComboBoxBeerPopupMenuWillBecomeInvisible

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new KegSplitFrame().setVisible(true);
            }
        });
    }

    public void LoadList(){
        DBA.openDBCon();
        Stock = DBA.RunSQLQuery("SELECT BIId,BIBId,BIHalf FROM BeerInventory WHERE BIHalf>'0'");
        jComboBoxBeer.addItem("");
        for(String s[] : Stock){
            DBA.GetBeerData(s[1]);
            String desc = DBA.BName + " - " + s[2];
            jComboBoxBeer.addItem(desc);
        }
        DBA.closeDBCon();

        jTextFieldNumHalf.setText("0");
        jTextFieldNumQtr.setText("0");
        jTextFieldNumSix.setText("0");
    }

    public void ResetScreen(){
        jComboBoxBeer.removeAllItems();
        LoadList();
    }

    @Action
    public void ExitKegSplit() {
        dispose();
    }

    @Action
    public void Submit() {
        //Check that everything is okay before proceeding.
        boolean kosher = true;

        int stock = Integer.parseInt(jLabelStock.getText());
        int nhalf = Integer.parseInt(jTextFieldNumHalf.getText());
        int nqtr = Integer.parseInt(jTextFieldNumQtr.getText());
        int nsix = Integer.parseInt(jTextFieldNumSix.getText());
        String Selection = jComboBoxBeer.getSelectedItem().toString();

        //First check that we have enough stock.
        if (nhalf > stock) kosher = false;
        if (kosher == false){
            int response = JOptionPane.showConfirmDialog(null,
                "Not enough beer in stock.",
                "Warning",JOptionPane.WARNING_MESSAGE);
            return;
        }
        //Now check that it has been moved appropriately. I.e. 2 1/4 for every 1/2, etc.
        double bbltot = (double)nhalf / (double)2;
        double bblqtr = (double)nqtr / (double)4;
        double bblsix = (double)nsix / (double)6;
        double remain = bbltot - bblqtr - bblsix;

        if ((remain > (double)0.01) || (remain < (double)-0.01)) kosher = false;

        int response = 1;

        if (kosher == false){
            response = JOptionPane.showConfirmDialog(null,
                "Quantities to not add up, continue anyway?",
                "Warning",JOptionPane.WARNING_MESSAGE);
        } else {
            response = JOptionPane.showConfirmDialog(null,
                "This will modify inventory, continue?",
                "Warning",JOptionPane.WARNING_MESSAGE);
        }
        if (response == 0){
            DBA.openDBCon();
            for(String s[] : Stock){
                DBA.GetBeerData(s[1]);
                String desc = DBA.BName + " - " + s[2];
                if(Selection.equals(desc)){
                    DBA.GetBeerInventoryData(s[0]);
                    //jLabelStock.setText(Integer.toString(DBA.BIHalf));
                    int half = DBA.BIHalf - nhalf;
                    int qtr = DBA.BIQtr + nqtr;
                    int six = DBA.BISix + nsix;

                    String query = "UPDATE BeerInventory SET BIHalf='" +
                        Integer.toString(half) + "',BIQtr='" +
                        Integer.toString(qtr) + "',BISix='" +
                        Integer.toString(six) +"' WHERE BIId = '" + s[0] + "'";
                    DBA.RunSQLUpdateQuery(query);
                }
            }
            DBA.closeDBCon();

            ResetScreen();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExit;
    private javax.swing.JButton jButtonSubmitMovement;
    private javax.swing.JComboBox jComboBoxBeer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabelStock;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField jTextFieldNumHalf;
    private javax.swing.JTextField jTextFieldNumQtr;
    private javax.swing.JTextField jTextFieldNumSix;
    // End of variables declaration//GEN-END:variables

}
